import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

MainView {
    id: paraCalView
    width: 800
    height: 391
    signal signalSuccess
    signal signalFailed
//    Connections{
//        target:paraCalSubView
//        onSelectedViewChanged: {
//            switch(paraCalSubView.currentViewIndex){
//                case 0:
//                    titleItem.text=qsTr("参数设置");
//                    break;
//                case 1:
//                    titleItem.text=qsTr("零点校准");
//                    break;
//                case 2:
//                    titleItem.text=qsTr("通道配置");
//                    break;
//                case 3:
//                    titleItem.text=qsTr("参数设置");
//                    break;
//                case 4:
//                    titleItem.text=qsTr("校准");
//                    break;
//            }
//        }
//    }
    Component.onCompleted: {
        paraCalSubView.currentViewIndex=4

    }
    onSignalSuccess: {
        switch(paraCalSubView.currentViewIndex){
            case 0:
                paraCalDialog.title=qsTr("保存成功，进入通道配置！")
                paraCalDialog.accepted.connect(function() {
                                        paraCalDialog.close();
                                        paraCalSubView.currentViewIndex = 3;
                                        titleItem.text=qsTr("通道配置");
                                    })
                paraCalDialog.open()
                break;
            case 1:
                paraCalDialog.title=qsTr("零点校准成功，进入增益校准！")
                paraCalDialog.accepted.connect(function() {
                                        paraCalDialog.close();
                                        paraCalSubView.currentViewIndex = 6;
                    titleItem.text=qsTr("增益校准");
                                    })
                paraCalDialog.open()
                break;
            case 2:
//                if(mymodbus.axisCount===0)
//                {
//                    if(gainCal.count===0)
//                    {
//                        gainCal.count++;
//                        titleItem.text=qsTr("校准")+gainCal.count+"/"+(mymodbus.axisCount===0?2:2*mymodbus.axisCount);
//                        mymodbus.clearMatrixData()
//                        mymodbus.appendMatrixData(gainCal.currentValue)
//                    }
//                    else if(gainCal.count===1)
//                    {

//                        gainCal.count=0
//                        mymodbus.appendMatrixData(gainCal.currentValue)
//                        var res= mymodbus.calculateFactor()
//                        if(res){
//                            paraCalDialog.title=qsTr("校准成功，完成设置标定！")
//                            paraCalDialog.accepted.connect(function() {
//                                                    paraCalDialog.close();
//                                                    myLoader.setsource="StatusView.qml"
//                                                    bottomBar.selected=0
//                                //titleItem.text=qsTr("通道配置");
//                                                })
//                            paraCalDialog.open()
//                        }
//                        else{
//                            gainCal.count = 0;
//                            titleItem.text=qsTr("校准")+"1/"+(mymodbus.axisCount===0?2:2*mymodbus.axisCount);
//                            paraCalDialog.title=qsTr("校准失败，请重试！")
//                            paraCalDialog.accepted.connect(function() {
//                                                    paraCalDialog.close();
//                                //titleItem.text=qsTr("通道配置");
//                                                })
//                            paraCalDialog.open()
//                        }
//                    }
//                }
//                else
                {
                    if(gainCalLoader.item.count===0)
                    {
                        gainCalLoader.item.count++;
                        titleItem.text=qsTr("校准")+(gainCalLoader.item.count+1)+"/"+(mymodbus.axisCount===0?2:2*mymodbus.axisCount);
                        mymodbus.clearMatrixData()
                        mymodbus.appendMatrixData(gainCalLoader.item.currentValue)
                    }
                    else if(gainCalLoader.item.count==mymodbus.axisCount*2-1)
                    {
                        gainCalLoader.item.count=0
                        mymodbus.appendMatrixData(gainCalLoader.item.currentValue)
                        var res= mymodbus.calculateFactor()
                        if(res){
                            paraCalDialog.title=qsTr("校准成功，完成设置标定！")
                            paraCalDialog.accepted.connect(function() {
                                                    paraCalDialog.close();
//                                myLoader.visible=false;
//                                myLoader.visible=true
                                                    myLoader.source="StatusView.qml"
                                                    bottomBar.selected=0
                                //titleItem.text=qsTr("通道配置");
                                                })
                            paraCalDialog.open()
                        }
                        else{
                            gainCalLoader.item.count = 0;
                            titleItem.text=qsTr("校准")+"1/"+(mymodbus.axisCount===0?2:2*mymodbus.axisCount);
                            paraCalDialog.title=qsTr("校准失败，请重试！")
                            paraCalDialog.accepted.connect(function() {
                                                    paraCalDialog.close();
                                //titleItem.text=qsTr("通道配置");
                                                })
                            paraCalDialog.open()
                        }

                    }
                    else
                    {
                        gainCalLoader.item.count++;
                        titleItem.text=qsTr("校准")+(gainCalLoader.item.count+1)+"/"+(mymodbus.axisCount===0?2:2*mymodbus.axisCount);
                        mymodbus.appendMatrixData(gainCalLoader.item.currentValue)
                    }
                }

                break;
            case 3:
                paraCalDialog.title=qsTr("通道设置成功，进入传感器参数设置！")
                paraCalDialog.accepted.connect(function() {
                    paraCalDialog.close();
                    paraCalSubView.currentViewIndex = 4;
                    titleItem.text=qsTr("传感器参数设置");
                })
                paraCalDialog.open()
                break;
            case 4:
                paraCalDialog.title=qsTr("传感器参数设置成功，进入零点标定！")
                paraCalDialog.accepted.connect(function() {
                    paraCalDialog.close();
                    paraCalSubView.currentViewIndex = 1;
                    titleItem.text=qsTr("零点标定")
                })
                paraCalDialog.open()
                break;
            case 5:
                paraCalDialog.title=qsTr("去皮成功，进入校准！")
                paraCalDialog.accepted.connect(function() {
                    paraCalDialog.close();
                    paraCalSubView.currentViewIndex = 2;
//                    gainCalLoader.item.count = 0;
//                    titleItem.text=qsTr("校准")+"1/"+(mymodbus.axisCount===0?2:2*mymodbus.axisCount);
                })
                paraCalDialog.open()
                break;
            case 6:
                paraCalDialog.title=qsTr("增益校准成功，完成设置标定！")
                paraCalDialog.accepted.connect(function() {
                                        paraCalDialog.close();
                                        myLoader.source="StatusView.qml"
                                        bottomBar.selected=0
                                    })
                paraCalDialog.open()
                break;
        }

    }
    onSignalFailed: {
        switch(paraCalSubView.currentViewIndex){
            case 0:
                paraCalDialog.title=qsTr("保存失败！")
                paraCalDialog.accepted.connect(function() {
                                        paraCalDialog.close();
                                    })
                break;
            case 1:
                paraCalDialog.title=qsTr("零点校准失败！")
                paraCalDialog.accepted.connect(function() {
                                        paraCalDialog.close();
                                    })
                break;
            case 2:
                paraCalDialog.title=qsTr("校准失败！")
                paraCalDialog.accepted.connect(function() {
                                        paraCalDialog.close();
                                    })
                break;
            case 3:
                paraCalDialog.title=qsTr("通道配置失败！")
                paraCalDialog.accepted.connect(function() {
                                        paraCalDialog.close();
                                    })
                break;
            case 4:
                paraCalDialog.title=qsTr("传感器参数设置失败！")
                paraCalDialog.accepted.connect(function() {
                                        paraCalDialog.close();
                                    })
                break;
            case 5:
                paraCalDialog.title=qsTr("去皮失败！")
                paraCalDialog.accepted.connect(function() {
                                        paraCalDialog.close();
                                    })
                break;
            case 6:
                paraCalDialog.title=qsTr("增益校准失败！")
                paraCalDialog.accepted.connect(function() {
                                        paraCalDialog.close();
                                    })
                break;
        }
        paraCalDialog.open()
    }
    function getBuzyVisible()
    {
        return paraBuzyIndicator.visible
                ||zeroCalBuzyIndicator.visible
                ||gainCalBuzyIndicator.visible
                ||weigherManagerBuzyIndicator.visible
                ||para1BuzyIndicator.visible
                ||clearZeroBuzyIndicator.visible
                ||paraCalLoader.visible
                ||gainCal1BuzyIndicator.visible;//
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: paraCalLoader
        visible:false
        z:101
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: paraBuzyIndicator
        visible:paraLoader.visible&&paraLoader.status!=Component.Ready
        z:101
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: zeroCalBuzyIndicator
        visible:zeroCalLoader.visible&&zeroCalLoader.status!=Component.Ready
        z:101
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: gainCalBuzyIndicator
        visible:gainCalLoader.visible&&gainCalLoader.status!=Component.Ready
        z:101
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: weigherManagerBuzyIndicator
        visible:weigherManagerLoader.visible&&weigherManagerLoader.status!=Component.Ready
        z:101
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: clearZeroBuzyIndicator
        visible:clearZeroLoader.visible&&clearZeroLoader.status!=Component.Ready
        z:101
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: paraCalBuzyIndicator
        visible:paraLoader.visible&&paraLoader.status!=Component.Ready
        z:101
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: para1BuzyIndicator
        visible:para1Loader.visible&&para1Loader.status!=Component.Ready
        z:101
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: gainCal1BuzyIndicator
        visible:gainCal1Loader.visible&&gainCal1Loader.status!=Component.Ready
        z:101
    }
    Dialog {
       anchors.centerIn: parent
       id: paraCalDialog
       standardButtons: Dialog.Ok
       //title:qsTr("保存成功！")
       modal:true
       closePolicy: "CloseOnEscape"
       onAccepted: {
           paraCalDialog.close()
           //if(normalSettingSaveOK){
               //bottomBar.selected = 0;
               //myLoader.visible=false;
               //statusView.visible=true;
               //myLoader.visible=false;
               //myLoader.visible=true;
               //myLoader.source="StatusView.qml"
           //}
       }
       z:100
    }
    Item{
        id:titleRec
        x: 0
        y: 0
        width:455
        height:44
        TextInput{
            id:titleItem
            anchors.fill: parent
            readOnly: true
            color:"white"
            font.pixelSize: 25
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignBottom
            text:qsTr("传感器参数设置")
            MouseArea {
                anchors.fill: parent
                anchors.leftMargin: 33
                anchors.rightMargin: -33
                anchors.topMargin: 13
                anchors.bottomMargin: -13
                onClicked: {
                    // miniKeyBoard.lineValue = "";
                       // miniKeyBoard.textInput=oldPwd;
                   }
               }

        }
    }


    RowLayout {
        x: 382
        y: 0
        Button {
            id: btnPre
            text: qsTr("上一步")
            font.pixelSize: 25
            Layout.preferredHeight: 50
            onClicked: {
                if(getBuzyVisible()){
                    return;
                }
                paraCalLoader.visible=true;
                switch(paraCalSubView.currentViewIndex){
                    case 0:
                        paraCalDialog.title=qsTr("已是第一步！")
                        paraCalDialog.open();
                        break;
                    case 1:
                        titleItem.text=qsTr("传感器参数设置");
                        paraCalSubView.currentViewIndex = 4
                        break;
                    case 2:
                        titleItem.text=qsTr("去皮，请清空车辆")
                        paraCalSubView.currentViewIndex = 5
                        break;
                    case 3:
                        titleItem.text=qsTr("参数设置");
                        paraCalSubView.currentViewIndex = 0
                        break;
                    case 4:
                        paraCalDialog.title=qsTr("已是第一步！")
                        paraCalDialog.open();
                        break;
                    case 5:
                        titleItem.text=qsTr("传感器参数设置");
                        paraCalSubView.currentViewIndex = 4
                        break;
                    case 6:
                        titleItem.text=qsTr("零点标定");
                        paraCalSubView.currentViewIndex = 1
                        break;
                }
                paraCalLoader.visible=false;
            }
            palette.buttonText: "#ffffff"
            background: Rectangle {
                color: "#40A0A0"
                border.width: 0
            }
            Layout.preferredWidth: 100
        }

        Button {
            id: btnOK
            text: qsTr("确定")
            font.pixelSize: 25
            Layout.preferredHeight: 50
            onClicked:{
                if(getBuzyVisible()){
                    return;
                }
                //paraCalLoader.visible=true;
                switch(paraCalSubView.currentViewIndex){
                    case 0:
                        paraLoader.item.signalOK();
                        break;
                    case 1:
                        zeroCalLoader.item.signalOK();
                        break;
                    case 2:
                        gainCalLoader.item.signalOK();
                        break;
                    case 3:
                        weigherManagerLoader.item.signalOK();
                        break;
                    case 4:
                        para1Loader.item.signalOK();
                        break;
                    case 5:
                        clearZeroLoader.item.signalOK();
                        break;
                    case 6:
                        gainCal1Loader.item.signalOK();
                        break;
                }
                //paraCalLoader.visible=false;
            }
            palette.buttonText: "#ffffff"
            background: Rectangle {
                color: "#c070b0"
                border.width: 0
            }
            Layout.preferredWidth: 100
        }

        Button {
            id: btnNext
            text: qsTr("下一步")
            font.pixelSize: 25
            Layout.preferredHeight: 50
            onClicked:{
                if(getBuzyVisible()){
                    return;
                }
                paraCalLoader.visible=true;
                switch(paraCalSubView.currentViewIndex){
                    case 0:
                        titleItem.text=qsTr("通道配置");
                        paraCalSubView.currentViewIndex=3
                        break;
                    case 1:
                        paraCalSubView.currentViewIndex=6
                        titleItem.text=qsTr("增益标定")
                        break;
                    case 2:
                        paraCalDialog.title=qsTr("已是最后一步！")
                        paraCalDialog.open();
                        //myLoader.setsource="StatusView.qml"
                        //bottomBar.selected=0
                        break;
                    case 3:
                        titleItem.text=qsTr("传感器参数设置");
                        paraCalSubView.currentViewIndex=4
                        break;
                    case 4:
                        titleItem.text=qsTr("零点标定")
                        paraCalSubView.currentViewIndex=1
                        break;
                    case 5:
                        titleItem.text=qsTr("校准")+"1/"+(2*mymodbus.axisCount);
                        paraCalSubView.currentViewIndex=2
                        break;
                    case 6:
                        paraCalDialog.title=qsTr("已是最后一步！")
                        paraCalDialog.open();
                        break;
                }
                paraCalLoader.visible=false;
            }
            palette.buttonText: "#ffffff"
            background: Rectangle {
               color: "#40A0A0"
               border.width: 0
            }
            Layout.preferredWidth: 100
        }
        Button {
            id: btnReturn
            text: qsTr("返回")
            font.pixelSize: 25
            Layout.preferredHeight: 50
            onClicked: {
//                myLoader.visible=false;
//                myLoader.visible=true
                myLoader.source="FunctionPanel.qml"
            }
            palette.buttonText: "#ffffff"
            background: Rectangle {
               color: "#b0b0d0"
               border.width: 0
            }
            Layout.preferredWidth: 100
        }
    }

   Item {
       id: paraCalSubView
       x: 8
       y: 45
       width: 784
       height: 338
       property int currentViewIndex: 4
       ParaCalSubItem {
           id: para
           visible:paraCalSubView.currentViewIndex === 0
//           enabled: paraCalSubView.currentViewIndex === 0
//           visible: opacity > 0.01
//           anchors.fill: parent
//           selectedView: paraCalSubView.currentViewIndex
//           index: 0
           Loader {
               anchors.fill:parent
               id: paraLoader
               asynchronous: true
               source:para.visible?"ParaCalParaSubItem.qml":""
               //source: "StatusView.qml"
              //sourceComponent: rect
           }
       }
       ParaCalSubItem {
           id: zeroCal
           visible:paraCalSubView.currentViewIndex === 1
//           enabled: paraCalSubView.currentViewIndex === 1
//           visible: opacity > 0.01
//           anchors.fill: parent
//           selectedView: paraCalSubView.currentViewIndex
//           index: 1
           Loader {
               anchors.fill:parent
               id: zeroCalLoader
               asynchronous: true
               source:zeroCal.visible?"ParaCalZeroCalSubItem.qml":""
               //source: "StatusView.qml"
              //sourceComponent: rect
           }
       }
       ParaCalSubItem {
           id: gainCal
           visible:paraCalSubView.currentViewIndex === 2
//           enabled: paraCalSubView.currentViewIndex === 2
//           visible: opacity > 0.01
//           anchors.fill: parent
//           selectedView: paraCalSubView.currentViewIndex
//           index: 2
           Loader {
               anchors.fill:parent
               id: gainCalLoader
               asynchronous: true
               source:gainCal.visible?"ParaCalGainCalSubItem.qml":""
               //source: "StatusView.qml"
              //sourceComponent: rect
               onLoaded:
               {
                   gainCalLoader.item.count = 0;
                   titleItem.text=qsTr("校准")+"1/"+(mymodbus.axisCount===0?2:2*mymodbus.axisCount);
               }
           }
       }
       ParaCalSubItem{
           id: weigherManager
           visible:paraCalSubView.currentViewIndex === 3
//           enabled: paraCalSubView.currentViewIndex === 3
//           visible: opacity > 0.01
//           anchors.fill: parent
//           selectedView: paraCalSubView.currentViewIndex
//           index: 3
           Loader {
               anchors.fill:parent
               id: weigherManagerLoader
               asynchronous: true
               source:paraCalSubView.visible?"ParaCalWeigherManager.qml":""
               //source: "StatusView.qml"
              //sourceComponent: rect
           }
       }
       ParaCalSubItem{
           id: para1
           visible:paraCalSubView.currentViewIndex === 4
//           enabled: paraCalSubView.currentViewIndex === 4
//           visible: opacity > 0.01
//           anchors.fill: parent
//           selectedView: paraCalSubView.currentViewIndex
//           index: 4
           Loader {
               anchors.fill:parent
               id: para1Loader
               asynchronous: true
               source:para1.visible?"ParaCalPara1SubItem.qml":""
               //source: "StatusView.qml"
              //sourceComponent: rect
           }
       }
       ParaCalSubItem{
           id: clearZero
           visible:paraCalSubView.currentViewIndex === 5
//           enabled: paraCalSubView.currentViewIndex === 5
//           visible: opacity > 0.01
//           anchors.fill: parent
//           selectedView: paraCalSubView.currentViewIndex
//           index: 5
           Loader {
               anchors.fill:parent
               id: clearZeroLoader
               asynchronous: true
               source:clearZero.visible?"ParaCalClearZero.qml":""
               //source: "StatusView.qml"
              //sourceComponent: rect

           }
       }
       ParaCalSubItem{
           id: gainCal1
           visible:paraCalSubView.currentViewIndex === 6
           Loader {
               anchors.fill:parent
               id: gainCal1Loader
               asynchronous: true
               source:gainCal1.visible?"ParaCalGainCal.qml":""
               //source: "StatusView.qml"
              //sourceComponent: rect

           }
       }
   }
}




